<!-- Uploader 上传组件二次封装 定义app内通用模板及验证 以便复用 -->
<template>
  <uploader
      class="styled-uploader"
      :show-list="false"
      action="http://123.57.138.48/api/upload/"
      :before-upload="commonUploadCheck"
      :on-success="onSuccessUpload"
  >
    <!-- default slot -->
    <div class="uploader-container">
      <FileImageOutlined/>
      <h4>{{ text }}</h4>
    </div>
    <!-- default loading -->
    <template #loading>
      <div class="uploader-container">
        <LoadingOutlined/>
        <h4>{{ text }}</h4>
      </div>
    </template>
    <!-- default slot -->
    <template #uploaded="{ uploadedData }">
      <div class="uploader-container">
        <img :src="uploadedData.url" v-if="showUploaded"/>
        <template v-else>
          <FileImageOutlined/>
          <h4>{{ text }}</h4>
        </template>
      </div>
    </template>
  </uploader>
</template>

<script lang='ts'>
import { defineComponent } from 'vue'
import Uploader, { SuccessData } from '@/components/Uploader.vue'
import { FileImageOutlined, LoadingOutlined } from '@ant-design/icons-vue'
import { commonUploadCheck } from '@/utils/helper'
import { UploadResp } from '../../extraType'

export default defineComponent({
  components: {
    Uploader,
    FileImageOutlined,
    LoadingOutlined
  },
  props: {
    // 上传按钮文案
    text: {
      type: String,
      default: '上传图片'
    },
    // 是否展示已上传后的图片
    showUploaded: {
      type: Boolean,
      default: false
    },

  },
  emits: ['success'],
  setup(props, { emit }) {

    const onSuccessUpload = (data: SuccessData) => {
      emit('success', data)
    }
    return {
      commonUploadCheck,
      onSuccessUpload
    }
  }
})
</script>

<style lang='scss' scoped>
.styled-uploader {
  .uploader-container {
    width: 100px;
    padding: 10px;
    color: #ffffff;
    background: #1890ff;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 5px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  }

  .uploader-container:hover {
    background: #40a9ff;
  }

  .uploader-container h4 {
    color: #ffffff;
    margin-bottom: 0;
    margin-left: 10px;
  }

  .uploader-container img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
</style>